package com.pqc.tbk.web;

import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pqc.tbk.entity.ConstProp;
import com.pqc.tbk.utils.CookiesCRUD;
import com.pqc.tbk.utils.Settings;
import com.pqc.tbk.utils.TaobaoUtil;
import com.pqc.tbk.utils.UserUtil;

/**
 * 
 * @author Ping_QC created on 2012-5-31 下午8:20:46
 * 
 */
@Controller
@RequestMapping("/")
public class IndexController {

	@RequestMapping({ "", "index" })
	public String index(final HttpServletResponse response,
			final String inviter, final ModelMap map) {
		map.put("inviter", inviter);

		final String app_key = Settings.Taobao.appkey;/* 填写appkey */
		final String secret = Settings.Taobao.appsecret;/* 填入Appsecret' */
		Long time = System.currentTimeMillis();
		while (time < 1000000000000L) {// 时间戳格式为13位数字类型
			time *= 10;
		}
		final String message = secret + "app_key" + app_key + "timestamp"
				+ time + secret;
		String mysign = "";
		try {
			mysign = TaobaoUtil.getHmacMd5Bytes(secret.getBytes(),
					message.getBytes());
		} catch (final NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mysign = mysign.toUpperCase();
		CookiesCRUD.addCookie(response, "timestamp", time + "", 0);
		CookiesCRUD.addCookie(response, "sign", mysign, 0);

		map.put("rate", ConstProp.Promote.BUYER_RETURN_RATE / 100);
		long uid = UserUtil.getUID();
		if (uid == -1) {
			uid = 1L;
		}
		map.put("outer_code", uid);

		return "index";
	}

	@RequestMapping("bottom")
	public String view() {
		return "bottom";
	}

}
